﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Sonce.Common;

namespace Sonce.Controls
{
    public partial class Console : System.Windows.Controls.Control
    {
        #region Members
        private List<string> messages = new List<string>();
        private bool isEnabled = true;
        #endregion

        #region Properties
        /// <summary>
        /// Return list of messages
        /// </summary>
        public List<string> Messages
        {
            get
            {
                return this.messages;
            }
        }

        /// <summary>
        /// Is this control enabled
        /// </summary>
        public new bool IsEnabled
        {
            get
            {
                return this.isEnabled;
            }
            set
            {
                this.isEnabled = value;
            }
        }

        public new double Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (pnlStack != null)
                {
                    pnlStack.Width = value;
                }
            }
        }

        public new double Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                if (pnlStack != null)
                {
                    pnlStack.Height = value;
                }
            }
        }
        #endregion

        #region Constructor
        public Console()
        {
            this.Loaded += new RoutedEventHandler(Control_Loaded);
        }
        #endregion

        #region Methods
        public void WriteLine(string message)
        {
            if (pnlStack != null)
            {
                TextBlock label = new TextBlock();
                label.Text = message;
                pnlStack.Children.Insert(0, label);
            }
        }

        public void Clear()
        {
            this.messages.Clear();
            pnlStack.Children.Clear();
        }
        #endregion

        #region Controls
        private FrameworkElement RootElement;
        private StackPanel pnlStack;

        public override void OnApplyTemplate()
        {
            RootElement = (FrameworkElement)GetTemplateChild("RootElement");
            pnlStack = (StackPanel)GetTemplateChild("pnlStack");
            this.Width = this.Width;
            this.Height = this.Height;
        }

        private void Control_Loaded(object sender, RoutedEventArgs e)
        {
            Loader.LoadStyle(this);
        }
        #endregion
    }
}
